from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, BadData
from django.conf import settings




def generate_save_user_token(openid):
    """对openid进行加密"""
    # 1. 创建序列化器对象
    serializer = Serializer(settings.SECRET_KEY, 600)
    data = {'openid': openid}
    # 调研序列化器的dumps
    access_token_bytes = serializer.dumps(data)

    # 3. 把加密后的openid返回
    return access_token_bytes.decode()


def check_save_user_token(openid):
    """对加密openid 进行解密"""
    # 创建序列化器对象
    serializer = Serializer(settings.SECRET_KEY, 600)
    try:
        # 调用loads方法对数据进行解密
        # openid = serializer.loads(openid)   # 取值为字典不合要求
        data = serializer.loads(openid)
    except BadData:   # ?
        return None
    else:
        return data.get('openid')